package com.irdstudio.efp.e4a.service.impl;

import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.efp.e4a.service.dao.SRuleSrcDao;
import com.irdstudio.efp.e4a.service.domain.SRuleSrc;
import com.irdstudio.efp.e4a.service.facade.SRuleSrcService;
import com.irdstudio.efp.e4a.service.vo.SRuleSrcVO;
import com.irdstudio.basic.framework.core.base.FrameworkService;

/**
 * <p>
 * ServiceImpl: 系统规则原表
 * <p>
 * 
 * @author common
 * @date 2018-05-03
 */
@Service("sRuleSrcService")
public class SRuleSrcServiceImpl implements SRuleSrcService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(SRuleSrcServiceImpl.class);

	@Autowired
	private SRuleSrcDao sRuleSrcDao;

	/**
	 * 新增操作
	 */
	public int insertSRuleSrc(SRuleSrcVO inSRuleSrcVo) {
		logger.debug("当前新增数据为:" + inSRuleSrcVo.toString());
		int num = 0;
		try {
			SRuleSrc sRuleSrc = new SRuleSrc();
			beanCopy(inSRuleSrcVo, sRuleSrc);
			num = sRuleSrcDao.insertSRuleSrc(sRuleSrc);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(SRuleSrcVO inSRuleSrcVo) {
		logger.debug("当前删除数据条件为:" + inSRuleSrcVo);
		int num = 0;
		try {
			SRuleSrc sRuleSrc = new SRuleSrc();
			beanCopy(inSRuleSrcVo, sRuleSrc);
			num = sRuleSrcDao.deleteByPk(sRuleSrc);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inSRuleSrcVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	public int updateByPk(SRuleSrcVO inSRuleSrcVo) {
		logger.debug("当前修改数据为:" + inSRuleSrcVo.toString());
		int num = 0;
		try {
			SRuleSrc sRuleSrc = new SRuleSrc();
			beanCopy(inSRuleSrcVo, sRuleSrc);
			num = sRuleSrcDao.updateByPk(sRuleSrc);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inSRuleSrcVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	public SRuleSrcVO queryByPk(SRuleSrcVO inSRuleSrcVo) {

		logger.debug("当前查询参数信息为:" + inSRuleSrcVo);
		try {
			SRuleSrc querySRuleSrc = new SRuleSrc();
			beanCopy(inSRuleSrcVo, querySRuleSrc);
			SRuleSrc queryRslSRuleSrc = sRuleSrcDao.queryByPk(querySRuleSrc);
			if (Objects.nonNull(queryRslSRuleSrc)) {
				SRuleSrcVO outSRuleSrcVo = beanCopy(queryRslSRuleSrc, new SRuleSrcVO());
				logger.debug("当前查询结果为:" + outSRuleSrcVo.toString());
				return outSRuleSrcVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SRuleSrcVO> queryAllOwner(SRuleSrcVO sRuleSrcVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		SRuleSrc sRuleSrc = beanCopy(sRuleSrcVo, new SRuleSrc());
		List<SRuleSrcVO> list = null;
		try {
			List<SRuleSrc> sRuleSrcs = sRuleSrcDao.queryAllOwnerByPage(sRuleSrc);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + sRuleSrcs.size());
			pageSet(sRuleSrcs, sRuleSrc);
			list = (List<SRuleSrcVO>) beansCopy(sRuleSrcs, SRuleSrcVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SRuleSrcVO> queryAllCurrOrg(SRuleSrcVO sRuleSrcVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		SRuleSrc sRuleSrc = beanCopy(sRuleSrcVo, new SRuleSrc());
		List<SRuleSrc> sRuleSrcs = sRuleSrcDao.queryAllCurrOrgByPage(sRuleSrc);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + sRuleSrcs.size());
		List<SRuleSrcVO> list = null;
		try {
			pageSet(sRuleSrcs, sRuleSrc);
			list = (List<SRuleSrcVO>) beansCopy(sRuleSrcs, SRuleSrcVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SRuleSrcVO> queryAllCurrDownOrg(SRuleSrcVO sRuleSrcVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		SRuleSrc sRuleSrc = beanCopy(sRuleSrcVo, new SRuleSrc());
		List<SRuleSrc> sRuleSrcs = sRuleSrcDao.queryAllCurrDownOrgByPage(sRuleSrc);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + sRuleSrcs.size());
		List<SRuleSrcVO> list = null;
		try {
			pageSet(sRuleSrcs, sRuleSrc);
			list = (List<SRuleSrcVO>) beansCopy(sRuleSrcs, SRuleSrcVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询所有规则信息
	 */
	@SuppressWarnings("unchecked")
	public List<SRuleSrcVO> querySRuleSrcAll(SRuleSrcVO sRuleSrcVo) {
		logger.debug("查询所有可用规则信息开始......");
		SRuleSrc sRuleSrc = beanCopy(sRuleSrcVo, new SRuleSrc());
		List<SRuleSrc> sRuleSrcs = sRuleSrcDao.querySRuleSrcAllByPage(sRuleSrc);
		logger.debug("查询所有可用规则信息结果集数量为:" + sRuleSrcs.size());
		List<SRuleSrcVO> list = null;
		try {
			pageSet(sRuleSrcs, sRuleSrc);
			list = (List<SRuleSrcVO>) beansCopy(sRuleSrcs, SRuleSrcVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

}
